import { _decorator, Component, Label, Node } from 'cc';
import { UIManager } from '../common/UIManager';
import AllyApi from '../api/AllyApi';
import { Ally, AllyOperateType } from '../entity/Ally';
import { AllyUser } from '../entity/AllyUser';
import { PrefabEnum } from '../common/PrefabEnum';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('AllyInfo')
export class AllyInfo extends Component {
    @property(Label)
    title: Label
    @property(Label)
    leader: Label
    @property(Label)
    elder: Label
    @property(Node)
    quitAlly: Node
   
    @property(Label)
    member: Label
    @property(Label)
    point: Label
    @property(Label)
    rank: Label
    @property(Label)
    field: Label
    @property(Label)
    avgPeople: Label
    @property(Label)
    job: Label
    @property(Label)
    people: Label
    @property(Label)
    myRank: Label
    @property(Label)
    villageNum: Label

    start() {
        this.quitAlly.on(Node.EventType.TOUCH_END, this.onQuitAlly, this)

        this.query()
    }

    async query() {
        await UIManager.instance.showLoading()
        let res = await AllyApi.Execute({
            op: AllyOperateType.Query
        })
        UIManager.instance.closeLoading()
        console.log(res)
        let data = res.data
        EventManager.instance.emit(EventManager.EventType.AllyRedDot, data)

        let ally = new Ally()
        ally.AllyId = data.allyId
        if (ally.isLeader) {
            this.quitAlly.active = false
        }

        let userList: AllyUser[] = []
        for (let item of data.ao.AllyUser) {
            userList.push(AllyUser.createByObj(item))
        }


        this.title.string = data.ao.allyName + '的联盟'
        let oldList = []
        let peopleTotal = 0
        for (let u of userList) {
            peopleTotal += u.People
            if (u.AllyTitle == '盟主') {
                this.leader.string = u.UserName
            }
            if (u.AllyTitle == '长老') {
                oldList.push(u.UserName)
            }
        }

        if (oldList.length > 20) {
            this.elder.string = oldList.slice(0, 20).join('、') + '...'
        } else {
            this.elder.string = oldList.join('、')
        }

        this.member.string = data.ao.memberNum + '/100'
        this.point.string = data.ao.point
        this.rank.string = data.ao.Rank
        this.field.string = data.ao.FieldNum
        this.avgPeople.string = (peopleTotal / userList.length).toFixed()

        this.job.string = data.me.AllyTitle
        this.people.string = data.me.People
        this.villageNum.string = data.me.VillageNum


    }

    async onQuitAlly() {
        try {
            await UIManager.instance.modal('提示', '您确定退出联盟吗？',)
            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.Quit,
            })
            UIManager.instance.closeLoading()

            UIManager.instance.closeUI(PrefabEnum.AllyIndex)
            UIManager.instance.openUI(PrefabEnum.AllyIndex)
        } catch (error) {

        }

    }
}


